local hx__qijing = fk.CreateSkill {
  name = "hx__qijing",
  tags = { Skill.Wake },
}

hx__qijing:addEffect(fk.TurnEnd, {
  priority = 2,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__qijing.name) and player:usedSkillTimes(hx__qijing.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data)
    return player:getMark("hx__tuoyu1") > 0 and player:getMark("hx__tuoyu2") > 0 and player:getMark("hx__tuoyu3") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.dead then return false end
    room:handleAddLoseSkills(player, "hx__cuixin")
    local tos = table.filter(room.alive_players, function (p)
      return p ~= player and p:getNextAlive(true) ~= player
    end)
    if #tos > 0 then
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        prompt = "#hx__qijing-choose",
        skill_name = hx__qijing.name,
        cancelable = true,
        no_indicate = true,
      })
      if #to > 0 then
        room:moveSeatTo(player, to[1].next.seat, false)
      end
    end
    player:gainAnExtraTurn(true, hx__qijing.name)
  end,
})

return hx__qijing
